home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
NetNews Offline 2
/
NetNews Offline Volume 2.iso
/
news
/
comp
/
std
/
c
/
403
< prev
next >
Wrap
Internet Message Format
|
1996-08-06
|
2KB
Path: solon.com!not-for-mail
From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
Newsgroups: comp.std.c,comp.lang.c.moderated
Subject: Re: Integral promotion.
Date: 15 Feb 1996 09:42:04 -0600
Organization: Carelcomp Forest
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4fvk8c$eq8@solutions.solon.com>
References: <4fstj7$2l6@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Mailer: Mozilla 2.0b6a (WinNT; I)
Rune Huseby wrote:
:
: I got a problem understanding the rules on integral promotion in
: the C language:
:
: Consider the following function:
:
: short test(short x1, short x2);
:
: int main(void)
: {
: short result;
: result = test(1, 2);
The numbers 1 and 2 here are by convention considered by compiler to be ints (not
shorts).
: return 0;
: }
:
: short test(short x1, short x2)
: {
: short result;
: result = x1 + x2; /* Warning: '=' : conversion from 'int '
: to 'short ', possible loss of data */
: return result;
: }
:
: My compiler (Microsoft Visual C++ 4.0), automagically converts
: my short-parameters to int's, even though all variables involved
: are short. I know that the standard says that all arguments can
: be converted to the biggest 'type' of all the arguments, but is
: it correct that char's and short's always are promoted to int's,
: without regard to the other arguments in the expression?
Chars are converted to ints, because passing a byte (where char equals one byte in
size) is both inefficient and leads to difficulties in the receiving party (for
instance, to pass two chars would then pack them into one byte, which the receiver
would have to be able to handle).
:
: The reason I ask is that I am writing code that should be
: portable from a 16-bits environment (where short and int are the
: same size) to a 32-bits environment. (My 16-bits compiler does
: not complain about the code).
That's because in 16-bits, sizeof(int) == sizeof(short) in PC world.
Later,
AriL
--
All my opinions are mine and mine alone.